package com.cheer.controller;

import com.cheer.dto.OrganizationAddDTO;
import com.cheer.dto.OrganizationUpdateDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.OrganizationService;
import com.cheer.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    /**
     * 查询联合审查机构
     */
    @GetMapping("/join")
    public Result<List<OrganizationTreeVO>> getJoin() {
        List<OrganizationTreeVO> treeList = organizationService.listJoinOrganizations();
        return Result.success(treeList);
    }

    /**
     * 查询管理员可见的机构
     */
    @GetMapping("/list")
    public Result<List<OrganizationTreeVO>> list() {
        List<OrganizationTreeVO> treeList = organizationService.listByTree(userInfoHolder.getAdminOrgId());
        return Result.success(treeList);
    }


    /**
     * 查询管理员可见的机构
     */
    @ApiOperation("参评机构信息")
    @GetMapping("/reference/list")
    public Result<List<OrganizationTreeVO>> referenceList() {
        List<OrganizationTreeVO> treeList = organizationService.referenceList(userInfoHolder.getAdminOrgId());
        return Result.success(treeList);
    }


    /**
     * 分页查询机构及机构文明号统计信息
     */
    @GetMapping("/page")
    public Result<Page<OrganizationPageVO>> page(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                 @RequestParam(required = false) Long orgId) {
        if (orgId == null) {
            orgId = userInfoHolder.getAdminOrgId();
        }
        Page<OrganizationPageVO> page = organizationService.getPage(pageNum, pageSize, orgId);
        return Result.success(page);
    }

    /**
     * 导出机构及机构文明号统计信息
     */
    @GetMapping("/download")
    public void download(@RequestParam(required = false) Long orgId) {
        if (orgId == null) {
            orgId = userInfoHolder.getAdminOrgId();
        }
        organizationService.download(orgId);
    }

    /**
     * 添加机构
     */
    @PostMapping("/add")
    public Result add(@RequestBody @Valid OrganizationAddDTO organizationAddDTO) {
        organizationService.add(organizationAddDTO);
        return Result.success();
    }

    /**
     * 修改机构
     */
    @PostMapping("/update")
    public Result update(@RequestBody @Valid OrganizationUpdateDTO organizationUpdateDTO) {
        organizationService.update(organizationUpdateDTO);
        return Result.success();
    }

    /**
     * 删除机构
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam Long orgId) {
        organizationService.delete(orgId);
        return Result.success();
    }

    /**
     * 上传机构文档
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        organizationService.upload(file);
        return Result.success();
    }


    /**
     * 上传机构 + 管理人 文档
     */
    // @PostMapping("/uploadOrgWithManager")
    public Result uploadOrgWithManager(MultipartFile file, Long pId) {
        organizationService.uploadOrgWithManager(file, pId);
        return Result.success();
    }


}
